package driver

import (
	"shen/gotask/rocketmq/config"
	"shen/gotask/rocketmq/driver/client"
	"shen/gotask/rocketmq/driver/service"
	"shen/gotask/rocketmq/logs"
	"sync"
	"time"
)

type Driver struct {
	Service         service.Service
	Client          client.Client
	DriverCloseChan chan bool
}

func (d *Driver) Run() {

	d.DriverCloseChan = make(chan bool)

	var wg sync.WaitGroup
	d.Service = service.Service{
		Config:             config.Config,
		DriverCloseChannel: d.DriverCloseChan,
		Logs:               logs.NewLogs(),
	}
	wg.Add(1)
	go d.Service.Run(&wg)

	d.Client = client.Client{
		Config:             config.Config,
		DriverCloseChannel: d.DriverCloseChan,
		Logs:               logs.NewLogs(),
	}

	wg.Add(1)
	go d.Client.Run(&wg)

	wg.Add(1)
	go func() {
		defer wg.Done()
		<-d.DriverCloseChan
		for i := 0; i < 2; i++ {
			wg.Done()
			time.Sleep(time.Millisecond * 200)
		}
	}()
	wg.Wait()

}
